<template>
  <div class="login-container">
    <el-card class="login-card">
      <h2 class="login-title">慢病回访系统</h2>
      <el-form :model="loginForm" :rules="rules" ref="loginForm" label-position="top">
        <el-form-item label="账号" prop="username">
          <el-input
              v-model="loginForm.username"
              prefix-icon="el-icon-user"
              placeholder="请输入账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
              v-model="loginForm.password"
              type="password"
              prefix-icon="el-icon-lock"
              placeholder="请输入密码"
              show-password
          ></el-input>
        </el-form-item>
        <div class="button-container">
          <el-form-item>
            <el-button type="primary" @click="handleLogin" :loading="loading">登录</el-button>
          </el-form-item>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { ElMessage } from 'element-plus';
import {apiClient} from './api/apiClient' // 引入 apiClient 实例

export default {
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
      },
      rules: {
        username: [
          { required: true, message: '请输入账号', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ],
      },
      loading: false,
    };
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true;
          // 调用后端登录接口
          apiClient.post('/doctor/login', {
            username: this.loginForm.username,
            password: this.loginForm.password,
          })
              .then((response) => {
                this.loading = false;
                if (response.data.code === 1) {
                  // 登录成功，处理返回的 token 和 authority
                  const token = response.data.data.token;
                  const authority = response.data.data.authority;
                  const user_id = response.data.data.id;
                  localStorage.setItem('token', token); // 将 token 存储到 localStorage
                  localStorage.setItem('authority', authority); // 将 authority 存储到 localStorage
                  localStorage.setItem('user_id', user_id);
                  console.log('user_id',localStorage.getItem('user_id'))
                  ElMessage.success('登录成功');
                  // 根据authority判断跳转路径
                  if (authority === '甲状腺疾病' || authority === '超级用户1') {
                    this.$router.push('/FirstView1'); // 跳转至新界面
                  } else {
                    this.$router.push('/FirstView'); // 保留原有跳转
                  }
                } else {
                  ElMessage.error(response.data.message || '登录失败');
                }
              })
              .catch((error) => {
                this.loading = false;
                ElMessage.error('登录失败，请稍后重试');
                console.error('登录失败:', error);
              });
        } else {
          ElMessage.error('表单验证失败');
        }
      });
    }
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f0f2f5;
}

.login-card {
  width: 400px;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  font-size: 24px;
  color: #333;
}

.button-container {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>